﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Company_DepartmentAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            this.BindDataToLanguageDropDown();
            this.BindDataToOfficeDropDown();
            this.SetLabelLanguage();
            this.InitData();
        }
    }

    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        lblDepartmentName.Text = MultipleLanguage.LBL_DEPARTMENT[nDefaultLanguage];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage];
        lblEmail.Text = MultipleLanguage.LBL_EMAIL[nDefaultLanguage];
        lblTel.Text = MultipleLanguage.LBL_TEL[nDefaultLanguage];
        lblOffice.Text = MultipleLanguage.LBL_DEPARTMENT[nDefaultLanguage];  
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage];
        btnBack.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage];
    }

    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();

        // Bind Data To Language
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    private void BindDataToOfficeDropDown()
    {
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);  
        ContactOffice obj = new ContactOffice();
        DataTable dtb = new DataTable();
        dtb= obj.Search();

        // Bind Data To Language
        ddlOffice.DataSource = dtb;
        ddlOffice.DataValueField = "ContactOfficeID";
        switch (nCurrentLang)
        { 
            case 1:
                ddlOffice.DataTextField = "Name1";
                break;
            case 2:
                ddlOffice.DataTextField = "Name2";
                break;
            case 3:
                ddlOffice.DataTextField = "Name2";
                break;
        }
       
        ddlOffice.DataBind();
    }
    private void InitData()
    {
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);

        try
        {
            if (sQueery == Config.ACTION_EDIT)
            {
                int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);



                if (nItemID > 0)
                    GetData(nCurrentLang, nItemID);

                Session[Config.SESSION_LANGID] = "1";
            }


        }
        catch
        {
            string sRedirect = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COMPANY  + "&" + Config.MESS_ID + "=" + Config.ITEM_NOTEXIST;
            Response.Redirect(sRedirect);
        }
    }

    private void GetData(int CurrentLang, int ItemID)
    {
        // Get data to view on UI Controls

        ContactDepartment objContactDepartment = new ContactDepartment();
        objContactDepartment.LoadById(ItemID);

        ddlLanguage.SelectedValue = ConvertData.ConvertToString(CurrentLang);
        txtEmail.Text = objContactDepartment.Data.Email;

        switch (CurrentLang)
        {
            case 1: // Vietnam
                txtTel.Text = objContactDepartment.Data.Tel1 ;
                txtDepartmentName.Text = objContactDepartment.Data.DepartmentName1 ;
                break;
            case 2: // English
               txtTel.Text = objContactDepartment.Data.Tel2 ;
                txtDepartmentName.Text = objContactDepartment.Data.DepartmentName2 ;
                break;
            case 3: // Other Lang
                txtTel.Text = objContactDepartment.Data.Tel3 ;
                txtDepartmentName.Text = objContactDepartment.Data.DepartmentName3 ;
                break;
        }
        ddlOffice.SelectedValue = ConvertData.ConvertToString(objContactDepartment.Data.ContactOfficeID);   

    }

    private eContactDepartment ContactDepartmentEntity()
    {
        eContactDepartment entityContactDepartment = new eContactDepartment();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            ContactDepartment objContactDepartment = new ContactDepartment();
            objContactDepartment.LoadById(nItemID);
            entityContactDepartment = objContactDepartment.Data;
        }
        return entityContactDepartment;
    }

    private eContactDepartment SetData(int CurrentLang)
    {
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        eContactDepartment eContactDepartmentEntity = this.ContactDepartmentEntity();
        ContactDepartment objContactDepartment = new ContactDepartment();

        objContactDepartment.Data.Email = txtEmail.Text.Trim();
        objContactDepartment.Data.ContactOfficeID = ConvertData.ConvertToInt(ddlOffice.SelectedValue); 
        switch (CurrentLang)
        {
            case 1:
                objContactDepartment.Data.DepartmentName1  = txtDepartmentName.Text.Trim();
                objContactDepartment.Data.Tel1  = txtTel.Text.Trim();


                objContactDepartment.Data.DepartmentName2 = ConvertData.ConvertToString(eContactDepartmentEntity.DepartmentName2 );
                objContactDepartment.Data.Tel2 = ConvertData.ConvertToString(eContactDepartmentEntity.Tel2 );

                objContactDepartment.Data.DepartmentName3 = ConvertData.ConvertToString(eContactDepartmentEntity.DepartmentName3 );
                objContactDepartment.Data.Tel3 = ConvertData.ConvertToString(eContactDepartmentEntity.Tel3 );

                break;
            case 2:
                objContactDepartment.Data.DepartmentName2  = txtDepartmentName.Text.Trim();
                objContactDepartment.Data.Tel2  = txtTel.Text.Trim();


                objContactDepartment.Data.DepartmentName1= ConvertData.ConvertToString(eContactDepartmentEntity.DepartmentName1);
                objContactDepartment.Data.Tel1 = ConvertData.ConvertToString(eContactDepartmentEntity.Tel1 );

                objContactDepartment.Data.DepartmentName3 = ConvertData.ConvertToString(eContactDepartmentEntity.DepartmentName3 );
                objContactDepartment.Data.Tel3 = ConvertData.ConvertToString(eContactDepartmentEntity.Tel3 );
                break;
            case 3:
               objContactDepartment.Data.DepartmentName3  = txtDepartmentName.Text.Trim();
                objContactDepartment.Data.Tel3  = txtTel.Text.Trim();


                objContactDepartment.Data.DepartmentName2 = ConvertData.ConvertToString(eContactDepartmentEntity.DepartmentName2 );
                objContactDepartment.Data.Tel2 = ConvertData.ConvertToString(eContactDepartmentEntity.Tel2 );

                objContactDepartment.Data.DepartmentName1 = ConvertData.ConvertToString(eContactDepartmentEntity.DepartmentName1 );
                objContactDepartment.Data.Tel1 = ConvertData.ConvertToString(eContactDepartmentEntity.Tel1 );
                break;
        }
        return objContactDepartment.Data;
    }

    private string GetQuerryString(string sQuerry)
    {
        string sQuerryString = "";
        sQuerryString = ConvertData.ConvertToString(Request.QueryString[sQuerry]);
        return sQuerryString;
    }



    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.InitData();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nActionResult = 0;
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nDefaultLanguage = Support.GetDefaultLanguage();
        ContactDepartment objContactDepartment = new ContactDepartment();
        objContactDepartment.Data = SetData(nCurrentLanguage);
        string sAction = GetQuerryString(Config.ACTION);
        if (sAction == Config.ACTION_EDIT)
        {
            //Is Edit
            int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
            nActionResult = objContactDepartment.Update(nItemID);
            if (nActionResult > 0)
            {
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage - 1]);

            }
            else
                Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage - 1]);
        }
        else
        {
            // Is Insert New

            nActionResult = objContactDepartment.Insert();
            if (nActionResult > 0)
            {
                string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_DEPARTMENT  + "&" + Config.MESS_ID + "=" + Config.MESSAGE_INSERT;
                Response.Redirect(sURL);
            }

        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_DEPARTMENT ;
        Response.Redirect(sURL);
    }
}